* ======================================
* ------ Harmonize knr's and bsu's -----
* ======================================

* Reconcile administrative changes in bsu's and knr's between 2015 and 2019.

* Municipalities
* ==============

	import excel "$dta_ext\knr_changes_20152019.xlsx", ///
		sheet("Sheet1") firstrow clear
	drop if knr_new == ""

	* Merged or changed id: use new designation
	gen knr_cngto = knr_new
	gen knr_cngfrom = knr_former

	keep knr_cng*
	gen knr_harmonize = 1
	save "$dta\harmonize_knr.dta",replace


* Basic statistical units
* =======================

	import excel "$dta_ext\bsu_changes_20152019.xlsx", ///
		sheet("Sheet1") firstrow clear
	drop if bsu_new == "" 
	drop if strpos(comment, "new bsuname") > 0 		// No need to harmonize
	
	gen bsu_cngto = ""
	gen bsu_cngfrom = ""

	* Case A: New knr id only -> use new designation
	replace bsu_cngto = bsu_new if strpos(comment, "new knr only") > 0
	replace bsu_cngfrom = bsu_former if strpos(comment, "new knr only") > 0

	* Case B: BSU splits -> use pre-split designation
	replace bsu_cngto = bsu_former if strpos(comment, "bsu split") > 0
	replace bsu_cngfrom = bsu_new if strpos(comment, "bsu split") > 0

	* Case C: complex changes, e.g., border adjustments (rare) -> drop
	replace bsu_cngto = "DROP" if bsu_cngto == ""
	expand 2 if bsu_cngto == "DROP"
	bysort bsu_former bsu_new: gen inx = _n if bsu_cngto == "DROP"
	replace bsu_cngfrom = bsu_former if inx == 1 	// set former to drop
	replace bsu_cngfrom = bsu_new if inx == 2 		// set new to drop
	bysort bsu_cngfrom bsu_cngto: drop if _n > 1 	// drop duplicates

	keep bsu_cng*
	gen bsu_harmonize = 1
	save "$dta\harmonize_bsu.dta",replace

